   package com.god.gl.blue;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * @author gl
 * @date 2021/2/1
 * @desc
 */
public class BlueBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "BlueBroadcastReceiver";
//    private String blueName = "Mi Silent Mouse";
    private String blueName = "小米蓝牙语音遥控器";
    private HidConnectUtil mHidConnectUtil;

    @Override
    public void onReceive(Context context, Intent intent) {
        mHidConnectUtil = HidConnectUtil.getInstance();
        String str = intent.getAction();
        if (null != str) {
            Log.e(TAG, "action:" + str);
            BluetoothDevice localBluetoothDevice = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (null != localBluetoothDevice) {
                Log.i(TAG, localBluetoothDevice.getName() + "==" + localBluetoothDevice.getAddress());
            }
            //监听扫描到蓝牙设备，过滤出所需蓝牙设备进行配对，如果之前配对过则直接连接。
            if (str.equals(BluetoothDevice.ACTION_FOUND) && null != localBluetoothDevice.getName()) {
                //发现设备
                if (null != localBluetoothDevice.getName() && localBluetoothDevice.getName().equals(blueName)) {
                    if (!mHidConnectUtil.isPair(localBluetoothDevice)) {
                        //没有配对先配对
                        mHidConnectUtil.pair(localBluetoothDevice);
                    } else {
                        //已配对直接链接
                        mHidConnectUtil.connect(localBluetoothDevice);
                    }
                }
                //"android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED"
            } else if (str.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
                //配对之后，连接设备可以通过监听下面的广播，判断设备是否连接成功
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, BluetoothAdapter.ERROR);
                Log.e("TAG", "state:" + state);
                if (state == BluetoothAdapter.STATE_CONNECTED) {//连接成功
                    Log.e(TAG, "连接成功");
                    mHidConnectUtil.stopScan();
                } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {//未连接
                    Log.e(TAG, "未连接");
                    // mHidConnectUtil.startScan();
                } else if (state == BluetoothAdapter.STATE_CONNECTING) {
                    Log.e(TAG, "连接中");
                }
            } else if (str.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) { //设备配对状态发生改变
                int connectState = localBluetoothDevice.getBondState();
                switch (connectState) {
                    case BluetoothDevice.BOND_NONE:
                        //未配对 删除配对
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        //配对中
                        break;
                    case BluetoothDevice.BOND_BONDED:
                        //配对成功链接
                        mHidConnectUtil.connect(localBluetoothDevice);
                        break;
                }

            } else if (str.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
                //蓝牙链接广播
                mHidConnectUtil.stopScan();
            } else if (str.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                //蓝牙断开广播
                mHidConnectUtil.startScan();
            }


        }
    }
}